package com.gxa.community.admin.scheduledTask;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gxa.community.admin.mapper.MessageIdMapper;
import com.gxa.community.core.pojo.domain.MessageId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
public class CleanMessageIdTask {

    @Autowired
    private MessageIdMapper messageIdMapper;

    @Scheduled(cron = "0 0 0 * * ?")
    public void cleanDBMessageIdTask(){
        //定时清除message_id表中的消息ID(每天0:00:00进行任务）
        //只清除消费时间比系统当前时间早一天以上的，
        long currentTime = System.currentTimeMillis();
        long timePoint = currentTime-1000*60*60*24;
        LambdaQueryWrapper<MessageId> messageIdLambdaQueryWrapper = Wrappers.lambdaQuery(MessageId.class);
        messageIdLambdaQueryWrapper.lt(MessageId::getTime,timePoint);
        messageIdMapper.delete(messageIdLambdaQueryWrapper);
    }
}
